﻿using System;
using System.Linq;
using System.Windows;
using DiagramDesigner.BusinessModelObject;
using System.Windows.Controls;

namespace DiagramDesigner
{
    /// <summary>
    /// Interaction logic for AskConnectionCriterion.xaml
    /// </summary>
    public partial class AskConnectionCriterion
    {
        #region Constructor
        public AskConnectionCriterion()
        {
            InitializeComponent();
        }
        #endregion

        #region Variables
        private string source;
        private string destination;
        #endregion

        #region Properties

        public BaseModel Item
        {
            get;
            set;
        }
        public DesignerCanvas Canvas
        {
            get; set;
        }
        //public Image LinkImage
        //{
        //    get { return image1; }
        //    set { image1 = value;}
        //}
        public LinkModel LinkClass
        {
            get; set;
        }
        public string Source
        {
            get { return source; }
            set { source = value; }
        }
        public string Destination
        {
            get { return destination; }
            set { destination = value; }
        }
        #endregion

        #region Events
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // realizando la conexion 
            if (ShowLink() == 1)
            {
                DialogResult = true;
                Close();
            }
            else
                Close();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ShowComboBox();
        }
        #endregion

        #region Methods
        //Connection GetConnection(string source, string destination)
        //{
        //    var iSource = GetDesignerItem(source);
        //    var iSink = GetDesignerItem(destination);

        //    var lados = new[] {"Left", "Top", "Right", "Bottom"};
        //    double distance = 0;
        //    Connector sourceConnector = null;
        //    Connector sinkConnector = null;


        //    for (var i = 0; i < lados.Count(); i++)
        //    {
        //        for (var j = 0; j < lados.Count(); j++)
        //        {
        //            var sourceC = Canvas.GetConnector(iSource.ID, lados[j], Canvas);
        //            var sink = Canvas.GetConnector(iSink.ID, lados[i], Canvas);

        //            var longitud = Math.Sqrt(Math.Pow(sourceC.Position.X - sink.Position.X, 2) +
        //                                     Math.Pow(sourceC.Position.Y - sink.Position.Y, 2));
        //            if((longitud<distance || distance==0 ) && (sourceC.Connections.Count==0 && sink.Connections.Count==0))
        //            {
        //                distance = longitud;
        //                //distance = 20;
        //                sourceConnector = sourceC;
        //                sinkConnector = sink;
        //                break;
        //            }
        //        }
        //    }
        //    var newCnn = new Connection(sourceConnector, sinkConnector, Item) {LinkClass = LinkClass};
        //    return newCnn;
        //}

        public int ShowLink()
        {
            if (comboBox2.Items.Count > 0)
            {
                if (comboBox2.SelectedIndex >= 0)
                {
                    //var nameSource = comboBox1.SelectedItem.ToString();
                    //this.source = nameSource;
                    var nameDestination = comboBox2.SelectedItem.ToString();
                    this.Destination = nameDestination;
                    //var newConnection = GetConnection(nameSource, nameDestination);

                    //Panel.SetZIndex(newConnection, Canvas.Children.Count);
                    //Canvas.Children.Add(newConnection);

                    return 1;
                }
                else
                {
                    MessageBox.Show("Please, select the Destination Element");
                    return 0;
                }
            }
            else
            {
                MessageBox.Show("There aren't Element");
                return 0;
            }
        }
        void ShowComboBox()
        {
            var designerItems = Canvas.Children.OfType<DesignerItem>();
            var enumerator = designerItems.GetEnumerator();
            //comboBox1.Items.Add(this.Name);
            while(enumerator.MoveNext())
            {
                var item = ((MainElementModel)enumerator.Current.Item);
                //comboBox1.Items.Add(item.Name);
                if (!item.GetType().Name.Equals("CriterionModel"))
                {
                    comboBox2.Items.Add(item.Name);
                }
            }
        }
        //DesignerItem GetDesignerItem(string name)
        //{
        //    var ie = Canvas.Children.OfType<DesignerItem>().Where(m => ((MainElementModel) m.Item).Name == name);
        //    var ge = ie.GetEnumerator();
        //    ge.MoveNext();
        //    var di = ge.Current;
        //    return di;
        //}
        #endregion
    }
}
